package etnorservice.etnor_service.controller;

import etnorservice.etnor_service.domain.po.User;
import etnorservice.etnor_service.domain.po.UserDetails;
import etnorservice.etnor_service.request.SearchUserRequest;
import etnorservice.etnor_service.request.UserDetailRequest;
import etnorservice.etnor_service.result.Result;
import etnorservice.etnor_service.service.UserService;
import etnorservice.etnor_service.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: cc
 * @Date: 2024/08/23/10:20
 **/
@RestController
public class UserController {
    @Autowired
    JwtTokenUtil jwtTokenUtil;
    @Autowired
    UserService userService;
        Result result=new Result();
        @GetMapping("/system/userList")
        public Result getUserList(
                @RequestParam(required = false) String account,
                @RequestParam(required = false) String username,
                @RequestParam(required = false) String status,
                @RequestParam(required = false) String role
) {
            // 创建一个查询对象
            SearchUserRequest query = new SearchUserRequest();
            query.setAccount(account.isEmpty()?null:account);
            query.setUsername(username.isEmpty()?null:username);
            query.setStatus(status.isEmpty()?null:status);
            query.setRole(role.isEmpty()?null:role);
            System.out.println("用户查询参数=======>"+query);
            // 调用服务层方法获取用户列表
//            List<User> userList = userService.getUserList(query);

            // 设置结果数据
//            result.setData(userList);
            result.setCode(200);
            result.setMessage("查询成功");

            return result;
        }
        @PostMapping("/user/details")
        public Result getUserDetails(@RequestBody UserDetailRequest userDetailRequest){
            Result result = new Result();
            UserDetails userDetails=new UserDetails();
            System.out.println("userDetailRequest"+userDetailRequest);
            if(userDetailRequest.getAccount()!=null&&userDetailRequest.getToken()!=null){
                if(jwtTokenUtil.validateToken(userDetailRequest.getToken())){
                    String validateToken=jwtTokenUtil.
                            getAccountFromToken(
                                    userDetailRequest.getToken());
                    if(userDetailRequest.getAccount().equals(validateToken)) {
                        System.out.println("验证成功");
                        result.setData(userService.findUserDetail(userDetailRequest.getAccount()));
                    }else{
                        //token解析账号与前端提交账号不一致
                    }
                }else {
                    //配置参数绕过一级拦截，判定高级侵入，予以记录
                }
            }else{
                //直接调用该接口，可判定侵入行为，予以警告
            }



            return result;
        }





    private String extractTokenFromHeader(String header) {
        if (header == null || !header.startsWith("Bearer ")) {
            return null;
        }
        return header.substring(7).trim(); // 去除 "Bearer ".length() 后面的空格
    }

}
